package api

import (
	"eat-every-day/internal/model"
	"eat-every-day/internal/routers/api/req_data_model"
	"eat-every-day/internal/service"
	"eat-every-day/pkg/errs"
	"github.com/gin-gonic/gin"
)

type usersHandler struct {
	Srv service.IUsersService
}

func (handler *usersHandler) UsernameIsExist(ctx *gin.Context) {
	username := ctx.Query("username")

	user, _ := handler.Srv.SelectUsersByUsername(username)
	if user.ID != 0 {
		model.SendResponse(ctx, errs.ErrUsername, nil)
		return
	}

	model.SendResponse(ctx, nil, nil)
}

func (handler *usersHandler) Register(ctx *gin.Context) {

	registerInfo := &req_data_model.RegisterUsers{}
	err := ctx.ShouldBind(&registerInfo)
	if err != nil {
		model.SendResponse(ctx, errs.ErrValidation, nil)
		return
	}

	users, _ := handler.Srv.SelectUsersByUsername(registerInfo.Username)
	if users.ID != 0 {
		model.SendResponse(ctx, errs.ErrInput, nil)
		return
	}

	err = handler.Srv.Create(registerInfo)
	if err != nil {
		model.SendResponse(ctx, errs.InternalServerError, nil)
		return
	}
	model.SendResponse(ctx, nil, nil)
}
